package com.kystar.muxplayer.selfview;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.PopupWindow;

import com.kystar.muxplayer.base.TLog;

/**
 * Created by peanut on 2018/8/16
 */
public class MyConstraintLayout extends ConstraintLayout {
    PopupWindow mPopupWindow;
    MyEditText mEditText;

    public MyConstraintLayout(Context context) {
        super(context);
    }

    public MyConstraintLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setPopupWindow(PopupWindow popupWindow) {
        mPopupWindow = popupWindow;
    }

    public void setEditText(MyEditText editText) {
        mEditText = editText;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mPopupWindow != null && mEditText != null) {
                    int[] locations = new int[2];
                    int ev_x = (int) ev.getRawX();
                    int ev_y = (int) ev.getRawY();
                    mEditText.getLocationOnScreen(locations);
                    int width = mEditText.getWidth();
                    int height = mEditText.getHeight();
                    if (ev_x > locations[0] && ev_x < (locations[0] + width) && ev_y > locations[1] && ev_y < locations[1] + height) {

                    } else {
                        mPopupWindow.dismiss();
                    }
                }
                break;
        }

        return super.dispatchTouchEvent(ev);
    }
}
